# Titulo: Map

rm(list = ls())
## llamando a las librerias que (creemos que) vamos a usar
options("install.lock"=FALSE)
if (!require ("pacman")) install.packages ("pacman")

pacman::p_load (dplyr
                , haven
                , ggplot2
                , ggpubr
                , ggthemes  ## si quieren mas themes
                , readstata13
                , readxl
                , sf
                , tidyverse
                , tidyr
                , units
                , viridis ## paleta de colores Viridis
                , wesanderson## p/usar paleta de colores de Wes Anderson
                , stringr
                , RColorBrewer
                , patchwork
                , Rmisc
                , lfe
                , stargazer
                , AER
                , haven
                , skimr
                , modelsummary
                , terra
                , fixest
                , vtable
                , did
                , cowplot
                , grid)

setwd("/Users/mateovasquez/Dropbox/")

#####

###MAPA

mapa_Comunidades <- st_read("./Comunidades_negras/Comunidades_negras.shp", stringsAsFactors=FALSE)
mapa_Municipios <- st_read("./Admin/COL_adm2.shp", stringsAsFactors=FALSE)
mapa_Departamentos <- st_read("./Admin/COL_adm1.shp", stringsAsFactors=FALSE)

table(mapa_Comunidades$ANO)

mapa_Comunidades$ANO[mapa_Comunidades$ANO == "2003 / 2012"] <- 2003

names(mapa_Departamentos)

color_sequence <- c("#FF8C00", "#FF4500", "#DC143C", "#FF1493", "#DA70D6", "#8A2BE2", "blue")
color_ramp <- colorRampPalette(color_sequence)

length(unique(mapa_Comunidades$ANO))

xlim <- c(-80, -73) # min and max longitude for a less zoomed-in view
ylim <- c(0, 9) # min and max latitude for a less zoomed-in view

pacific_bbox <- st_sfc(st_polygon(list(rbind(
  c(xlim[1], ylim[1]),
  c(xlim[1], ylim[2]),
  c(xlim[2], ylim[2]),
  c(xlim[2], ylim[1]),
  c(xlim[1], ylim[1])
))), crs = st_crs(mapa_Departamentos))



# All Colombia

inset_map <- ggplot() +
  geom_sf(data = mapa_Departamentos, fill = "white") + # Base layer with all regions in grey
  geom_sf(data = pacific_bbox, fill = "white", color = "red", alpha = .2) +
  theme_void() # Remove all theme elements for the inset map

Main_map <-ggplot(data = mapa_Departamentos) +
  geom_sf(fill = "white") +
  geom_sf(data = mapa_Comunidades, aes(fill = as.factor(ANO))) +
  scale_fill_manual(values = color_ramp(22), na.translate = FALSE) +
  coord_sf(xlim = xlim, ylim = ylim, expand = FALSE)  +
  theme_void() +
  theme(legend.position = "left") +
  guides(fill = guide_legend(title = "Resolution Year", na.translate = FALSE,ncol = 1))

inset_grob <- ggplotGrob(inset_map)

width <- unit(4, "cm")  # Adjust width as needed
height <- unit(4, "cm")  # Adjust height as needed

# Modify the grob's width and height
inset_grob$widths <- rep(width, length(inset_grob$widths))
inset_grob$heights <- rep(height, length(inset_grob$heights))

# These are rough estimates and will likely need adjusting
xmin <- min(xlim) - diff(xlim) * .2 # Slightly outside the left bound
ymin <- max(ylim) - diff(ylim) * 0.1   # Slightly below the top bound
xmax <- xmin + diff(xlim) * 0.2        # Ensure xmax > xmin
ymax <- ymin + diff(ylim) * 0.2        # Ensure ymax > ymin

Main_map_final <- Main_map + 
  annotation_custom(grob = inset_grob, xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax)

